package com;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * @Classname ATMSystem
 * @Description ATM系统
 * @Date 2022/1/13 12:15
 * @Created by 666
 */
public class ATMSystem {
    public static void main(String[] args) {
        ArrayList<Account> accounts = new ArrayList<>();
        //准备系统的首页：1:登录，2：开户
        showMain(accounts);
    }

    /*
     *@Description 展示首页
     *@return void
     *@date
     *@auther 666
     */
    public static void showMain(ArrayList<Account> accounts) {
        System.out.println("===================");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入您的操作：");
            System.out.println("1、登录");
            System.out.println("2、开户");
            System.out.println("您可以输入您的指令：");
            int cases = scanner.nextInt();
            switch (cases) {
                case 1:
                    //登录
                    login(accounts, scanner);
                    break;
                case 2:
                    //开户
                    register(accounts, scanner);
                    break;
                default:
                    System.out.println("您输入的操作有误，请重新输入");
            }
        }
    }

    private static void login(ArrayList<Account> accounts, Scanner scanner) {
        if (accounts.size() == 0) {
            System.out.println("账户信息不存在，请先注册");
            return;
        }
        while (true) {
            System.out.println("请输入您的登录卡号");
            String cardId = scanner.next();
            Account account = getAccountByCardId(cardId, accounts);
            //判断账户是否存在，存在输入密码
            if (account != null) {
                while (true) {
                    System.out.println("请输入您的登录密码");
                    String passWord = scanner.next();
                    if (passWord.equals(account.getPassWord())) {
                        //密码相同，然后登录成功，展示操作信息
                        System.out.println("恭喜您登录成功，您的卡号是：" + account.getCustId());
                        showProduct(account, scanner);
                        return;
                    } else {
                        System.out.println("您的密码不正确，请重新输入");
                    }
                }
            } else {
                System.out.println("对不起你的账户不存在，请重新输入");
            }
        }
    }

    /*
     *@Description 展示登录后的信息
     *@param  [account, scanner]
     *@return void
     *@date
     *@auther
     */
    private static void showProduct(Account account, Scanner scanner) {
        System.out.println("=========展示页面=============");
        System.out.println("1、查询账户");
        System.out.println("2、存款");
        System.out.println("3、取款");
        System.out.println("4、转账");
        System.out.println("5、修改密码");
        System.out.println("6、退出");
        System.out.println("7、注销账户");
        System.out.println("请输入您的命令");
        while (true) {
            int cases = scanner.nextInt();
            switch (cases) {
                case 1://查询账户
                    queryAccount(account);
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    System.out.println("退出账户");
                    return;
                case 7:
                    break;
                default:
                    System.out.println("您输入的指令有误请重新输入");

            }
        }
    }
    /*
     *@Description 展示账户信息
     *@param  [account, scanner]
     *@return void
     *@date
     *@auther
     */
    private static void queryAccount(Account account) {
        System.out.println("======用户信息==========");
        System.out.println("卡号："+account.getCustId());
        System.out.println("姓名："+account.getUserName());
        System.out.println("余额："+account.getMoney());
        System.out.println("当日转存金额："+account.getQuotaMoney());
    }

    /*
     *@Description  用户开户功能
     *@param  [accounts]
     *@return void
     *@date
     *@auther
     */
    public static void register(ArrayList<Account> accounts, Scanner scanner) {
        System.out.println("========用户开户功能===========");
        System.out.println("请您输入开户名称：");
        String userName = scanner.next();
        String passWord = "";
        String okPassWord = "";
        while (true) {
            System.out.println("请输入您的密码");
            passWord = scanner.next();
            System.out.println("请再次输入您的密码");
            okPassWord = scanner.next();
            if (passWord.equals(okPassWord)) {
                break;
            } else {
                System.out.println("两次密码输入不一致请重新输入");
            }
        }
        System.out.println("请输入当日限额：");
        double quotaMoney = scanner.nextDouble();
        //生产一个卡号 不能重复
        String cardNo = createCardNo(accounts);
        //创建对象
        Account account = new Account(cardNo, userName, passWord, quotaMoney);
        accounts.add(account);
        System.out.println("开户成功,您的卡号为：" + account.getCustId());
    }

    /*
     *@Description  生产一个卡号
     *@param  [accounts]
     *@return void
     *@date
     *@auther
     */
    private static String createCardNo(ArrayList<Account> accounts) {
        while (true) {
            //生成一个卡号
            String cardNo = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++) {
                cardNo += r.nextInt(10);
            }
            //判断卡号是否重复
            Account account = getAccountByCardId(cardNo, accounts);
            if (account == null) {
                return cardNo;
            }
        }
    }

    private static Account getAccountByCardId(String cardNo, ArrayList<Account> accounts) {
        for (int i = 0; i < accounts.size(); i++) {
            Account account = accounts.get(i);
            if (cardNo.equals(account.getCustId())) {
                return account;
            }
        }
        return null;
    }
}
